﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication2
{

    /// <summary>
    /// 依赖的数据源，需要在set的时触发PropertyChanged事件
    /// </summary>
    public class Person : INotifyPropertyChanged
    {
        private string pname;
        public event PropertyChangedEventHandler PropertyChanged;
        public Person() { }

        public string PersonName
        {
            get { return pname; }

            set
            {
                pname = value;
                OnPropertyChanged("PersonName");
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

  public  enum Sex
    {
        Male,
        Female
    }

public    class Personal
    {
        public string Name { get; private set; }
        public int Age { get; private set; }
        public Sex Sex { get; private set; }

        public Personal(string name, int age, Sex sex)
        {
            this.Name = name;
            this.Age = age;
            this.Sex = sex;
        }
    }
    /// <summary>
    /// 实现了直接渲染list的的功能，c# 提供内存
    /// </summary>
    public class PersonalList : ObservableCollection<Personal>
    {
        public PersonalList GetPersonals()
        {
            this.Add(new Personal("Tom", 15, Sex.Male));
            this.Add(new Personal("Mary", 11, Sex.Female));
            this.Add(new Personal("Jack", 13, Sex.Male));

            return this;
        }
    }



    /// <summary>
    /// 属性依赖的三种实现,xml,代码，
    /// </summary>
    public partial class WindowDependencyProperty : Window
    {
        public WindowDependencyProperty()
        {
            InitializeComponent();
            Binding binding = new Binding();
            binding.Source = per;
            binding.Path = new PropertyPath("PersonName");
            textBox2.SetBinding(TextBox.TextProperty, binding);
        }
        public static Person per = new Person() { PersonName = "test" };

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            per.PersonName += "_s";
        }

        private void listbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox list = (ListBox)sender;
            Personal item = (Personal)list.SelectedItem;
            if(item!=null)
            MessageBox.Show("name="+item.Name, "s");
        }
    }
}
